package cn.escco.ocr.service.impl;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.springframework.beans.factory.annotation.Value;

import com.alibaba.excel.util.StringUtils;

import cn.escco.ocr.service.CallPythonService;

public class CallPythonServiceImpl implements CallPythonService {
    
    @Value("${pdf.pyfilePath}")
    private String pyfilePath;
    
    @Value("${pdf.tempPdfPath}")
    private String tempPdfPath;

    @Override
    public String readPdf(String pdfFilePath) {
        
        return runPython("readPdf.py", pdfFilePath);
    }

    @Override
    public String analysisSa(String saImagePath) {
        return runPython("analysisSa.py", saImagePath);
    }

    @Override
    public String analysisWb(String wbImagePath) {
        return runPython("analysisWb.py", wbImagePath);
    }

    @Override
    public String analysisBl(String blImagePath) {
        return runPython("analysisBl.py", blImagePath);
    }

    @Override
    public String analysisIn(String inImagePath) {
        // TODO Auto-generated method stub
        return runPython("analysisIn.py", inImagePath);
    }
    
    private String runPython(String pythonFileName, String param ) {
        String returnString = StringUtils.EMPTY;
        try {
            String exe = "python";
            String py = pyfilePath + pythonFileName;
            String [] args = new String[] {exe, py, param};
            Process process = Runtime.getRuntime().exec(args);
            //获取结果的同时设置输入流编码格式"gb2312"
            InputStreamReader isr = new InputStreamReader(process.getInputStream(),"gb2312");
            InputStreamReader errr = new InputStreamReader(process.getErrorStream(),"gb2312");
            LineNumberReader input = new LineNumberReader(errr);
            returnString = input.readLine();
            input.close();
            isr.close();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnString;
    }

}
